import matplotlib.pyplot as plt

# 数据
groups = ["Group 1 (id=1~9)", "Group 2 (id=11~19)", "Group 3 (id=21~29)", "Group 4 (id=31~39)"]
accuracy = [100.00, 77.78, 44.44, 66.67]  # 正确率
incorrect_counts = [0, 2, 5, 3]  # 不正确的数据项数量

# 创建条形图
fig, ax = plt.subplots(figsize=(10, 6))

# 绘制正确率的条形图
bars1 = ax.bar(groups, accuracy, color='skyblue', label='Accuracy (%)')

# 在每个条形上显示正确率的值
for bar in bars1:
    height = bar.get_height()
    ax.annotate(f'{height:.2f}%',
                xy=(bar.get_x() + bar.get_width() / 2, height),
                xytext=(0, 3),  # 3 points vertical offset
                textcoords="offset points",
                ha='center', va='bottom')

# 绘制不正确的数据项数量的条形图
ax2 = ax.twinx()  # 创建第二个 y 轴
bars2 = ax2.bar(groups, incorrect_counts, color='lightcoral', alpha=0.6, label='Incorrect Items')

# 在每个条形上显示不正确的数据项数量
for bar in bars2:
    height = bar.get_height()
    ax2.annotate(f'{height}',
                 xy=(bar.get_x() + bar.get_width() / 2, height),
                 xytext=(0, 3),  # 3 points vertical offset
                 textcoords="offset points",
                 ha='center', va='bottom')

# 设置图表标题和标签
ax.set_xlabel('Groups')
ax.set_ylabel('Accuracy (%)')
ax2.set_ylabel('Incorrect Items')
ax.set_title('Accuracy and Incorrect Items by Group')

# 合并图例
lines, labels = ax.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax.legend(lines + lines2, labels + labels2, loc='upper left')

# 显示图表
plt.tight_layout()
plt.show()